<?php

namespace App\Models;


class MemberWaterBillLog extends BaseModel
{
    protected $table = "member_waterbill_log";

    protected $rules = [
        'order_sn' => 'required',
        'admin_id' => 'required',
        'shop_id' => 'required',
        'mid' => 'required',
        'waterbill_id' => 'required',
        'goods_id' => 'required',
        'goods_name' => 'required',
//        'order_amount' => ['required', 'numeric', 'regex:/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])/'],
        'order_amount' => ['required', 'numeric'],
        'num' => 'required|numeric',
    ];

    protected $message = [
        'order_sn.required' => '订单号获取失败',
        'admin_id.required' => '商户ID获取失败',
        'shop_id.required' => '店铺ID获取失败',
        'mid.required' => '用户ID获取失败',
        'waterbill_id.required' => '水票ID获取失败',
        'goods_id.required' => '商品ID获取失败',
        'goods_name.required' => '商品名称获取失败',
        'order_amount.required' => '订单总价格获取失败',
        'order_amount.numeric' => '订单总价格必须是纯数字',
//        'order_amount.regex' => '订单总价格不能小于0',
        'num.required' => '剩余总数量获取失败',
        'num.numeric' => '剩余总数量必须是纯数字',
    ];


    public function log($orderInfo,$goods,$type)
    {
        //$type 1.线上买水票  2.线下买水票 3.水票消费
        switch ($type) {
            case 1:
                $dataType = 0;
                $dataPayType = 20;
                $description = '购买';
                $useType = 0;
                $data['num'] = $orderInfo['total_num'];
                $data['buy_num'] = $orderInfo['buy_num'];
                $data['gift_num'] = $orderInfo['gift_num'];
                break;
            case 2:
                $dataType = 1;
                $dataPayType = 10;
                $description = '购买';
                $useType = 0;
                $data['num'] = $orderInfo['total_num'];
                $data['buy_num'] = $orderInfo['buy_num'];
                $data['gift_num'] = $orderInfo['gift_num'];
                break;
            case 3:
                $dataType = 0;
                $dataPayType = 20;
                $description = '消费';
                $useType = 1;
                $data['num'] = $goods['goods_num'];
                break;
            case 4:
                $dataType = 1;
                $dataPayType = 20;
                $description = '导入';
                $useType = 0;
                $data['buy_num'] = $goods['goods_num'];
                $data['num'] = $goods['goods_num'];
                break;
            case 5:
                $dataType = 1;
                $dataPayType = 20;
                $description = '充值';
                $useType = 0;
                $data['buy_num'] = $goods['goods_num'];
                $data['num'] = $goods['goods_num'];
                break;
            case 6:
                $dataType = 1;
                $dataPayType = 20;
                $description = '退还';
                $useType = 0;
                $data['buy_num'] = $goods['goods_num'];
                $data['num'] = $goods['goods_num'];
                break;
        }

        $data['order_sn'] = $orderInfo['order_sn'];
        $data['admin_id'] = $orderInfo['admin_id'];
        $data['shop_id'] = $orderInfo['shop_id'];
        $data['mid'] = $orderInfo['buyer_id'];
        $data['waterbill_id'] = $orderInfo['bill_id'];
        $data['description'] = $description;
        $data['goods_id'] = $goods['goods_id'];
        $data['goods_name'] = $goods['goods_name'];
        $data['order_amount'] = $orderInfo['order_amount'];
        $data['use_type'] = $useType;
        $data['type'] = $dataType;
        $data['pay_status'] = $dataPayType;
        $data['addtime'] = time();
        if (!$this->_validate($data)) {
            return false;
        }
        $res = $this->_save($data);
        if (!$res) {
            $this->error = '记录水票明细失败！';
            return false;
        }
        return $res;
    }

}
